﻿//-----------------------------------------------------------------------
// <copyright file="AttributeCollection.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Xml
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Xml;
    using System.Globalization;

    /// <summary>
    /// List of <see cref="System.Xml.XmlAttribute"/> for an xml node.
    /// </summary>
    [Serializable]
    public class AttributeCollection : Collection<XmlAttribute>
    {
        /// <summary>
        /// Gets the <see cref="System.Xml.XmlAttribute"/> with the specified attribute name.
        /// </summary>
        /// <param name="attributeName">The name of the attribute to get.</param>
        public XmlAttribute this[string attributeName]
        {
            get
            {
                return this.Items.FirstOrDefault(a => a.LocalName == attributeName);
            }
        }

        /// <summary>
        /// Adds the attribute with the specified specified name of the attribute, value, namespace prefix, and
        /// the namespace.
        /// </summary>
        /// <param name="namespacePrefix">The namespace prefix.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="attributeValue">The attribute value.</param>
        /// <param name="namespaceUri">The namespace URI.</param>
        public void AddAttribute(string namespacePrefix, string attributeName, string attributeValue, Uri namespaceUri)
        {
            var doc = new XmlDocument();
            XmlAttribute newAttribute = doc.CreateAttribute(namespacePrefix, attributeName, namespaceUri.ToString());
            newAttribute.Value = attributeValue;
            this.Add(newAttribute);
        }
    }
}
